package com.wswy.wyjk.ui.practiceNew;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qq.e.ads.banner2.UnifiedBannerView;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.ads.nativ.NativeExpressMediaListener;
import com.qq.e.comm.constants.AdPatternType;
import com.qq.e.comm.util.AdError;

import java.util.List;

public class NativeExpressADController {
    private Activity context;
    private ViewGroup bannerContainer;
    private NativeExpressAD nativeExpressAD;
    private String posId;
    private NativeExpressADView nativeExpressADView;


    public NativeExpressADController(Activity context, ViewGroup bannerContainer, String posId, boolean isSHow){
        this.context=context;
        this.bannerContainer=bannerContainer;
        this.posId=posId;
        if(isSHow) {
            refreshAd();
        }
    }

    public void onDestroy() {
        // 使用完了每一个NativeExpressADView之后都要释放掉资源
        if (nativeExpressADView != null) {
            nativeExpressADView.destroy();
        }
    }

    private void refreshAd() {
        try {

            /**
             *  如果选择支持视频的模板样式，请使用{@link PositionId#NATIVE_EXPRESS_SUPPORT_VIDEO_POS_ID}
             */
            nativeExpressAD = new NativeExpressAD(context,  new ADSize(500, ADSize.AUTO_HEIGHT), posId, nativeExpressADListener);
            nativeExpressAD.setVideoOption(new VideoOption.Builder()
                    .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
                    .setAutoPlayMuted(true) // 自动播放时为静音
                    .build());
            nativeExpressAD.setVideoPlayPolicy(VideoOption.VideoPlayPolicy.AUTO);
            nativeExpressAD.loadAD(1);
        } catch (NumberFormatException e) {
        }
    }


    private com.qq.e.ads.nativ.NativeExpressAD.NativeExpressADListener nativeExpressADListener=new  com.qq.e.ads.nativ.NativeExpressAD.NativeExpressADListener(){

        @Override
        public void onNoAD(AdError adError) {

        }

        @Override
        public void onADLoaded(List<NativeExpressADView> adList) {
            bannerContainer.setVisibility(View.VISIBLE);
            if (nativeExpressADView != null) {
                nativeExpressADView.destroy();
            }
            nativeExpressADView = adList.get(0);
            if (nativeExpressADView.getBoundData().getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
                nativeExpressADView.setMediaListener(mediaListener);
            }
            nativeExpressADView.render();
            if (bannerContainer.getChildCount() > 0) {
                bannerContainer.removeAllViews();
            }
            // 需要保证 View 被绘制的时候是可见的，否则将无法产生曝光和收益。
            bannerContainer.addView(nativeExpressADView);
        }

        @Override
        public void onRenderFail(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onRenderSuccess(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onADExposure(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onADClicked(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onADClosed(NativeExpressADView nativeExpressADView) {
            bannerContainer.setVisibility(View.GONE);
        }

        @Override
        public void onADLeftApplication(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {

        }
    };

    private NativeExpressMediaListener mediaListener = new NativeExpressMediaListener() {
        @Override
        public void onVideoInit(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoLoading(NativeExpressADView nativeExpressADView) {
        }

        @Override
        public void onVideoCached(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoReady(NativeExpressADView nativeExpressADView, long l) {
        }

        @Override
        public void onVideoStart(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoPause(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoComplete(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoError(NativeExpressADView nativeExpressADView, AdError adError) {
        }

        @Override
        public void onVideoPageOpen(NativeExpressADView nativeExpressADView) {
        }

        @Override
        public void onVideoPageClose(NativeExpressADView nativeExpressADView) {
        }
    };
}
